package com.smartcommunity.service.common.impl;

import com.alibaba.fastjson.JSONObject;
import com.smartcommunity.dao.common.SystemDao;
import com.smartcommunity.entity.system.FireIndex;
import com.smartcommunity.entity.system.SafetyIndex;
import com.smartcommunity.entity.system.SysParams;
import com.smartcommunity.service.common.SystemService;
import com.smartcommunity.utils.ToolDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: JiangM
 * @Date: 2018/4/18
 * @Description: 系统相关的ServiceImpl
 * @Modified By:
 */
@Service
@Slf4j
public class SystemServiceImpl implements SystemService {

	@Autowired
	private SystemDao systemDao;

    /**
     * 系统参数
     * @return
     */
	@Override
	public List<JSONObject> getSysParams() {
	    List<JSONObject> jsonObjList = new ArrayList<>();
        JSONObject jsonValue = new JSONObject();
        JSONObject jsonName = new JSONObject();
        //获取系统参数
        List<SysParams> SysParamsList = systemDao.getSysParamsByPage();
        for(SysParams sysParams:SysParamsList){
            jsonValue.put(sysParams.getKey(),sysParams.getValue());
            jsonName.put(sysParams.getKey(),sysParams.getName());
        }

        jsonObjList.add(jsonValue);
        jsonObjList.add(jsonName);
        
        return jsonObjList;
	}

    /**
     * 更新系统参数
     * @param paramMap
     * @return
     */
    @Override
    public JSONObject updateSysParam(Map<String, Object> paramMap) {
        JSONObject jsonObject = new JSONObject();

        try {
            paramMap.put("mrowtime", ToolDateTime.getFormatDate());
            systemDao.updateSysParam(paramMap);
            jsonObject.put("status", "success");
            jsonObject.put("message", "操作成功");

        } catch (Exception e) {
            jsonObject.put("status", "failure");
            jsonObject.put("message", e.getMessage());
            log.error("SystemServiceImpl:updateSysParam error ,param:{} ,e:",paramMap,e);
        }

        return jsonObject;
    }

    @Override
    public JSONObject queryAllIndexService(String type) {
        SafetyIndex safety=new SafetyIndex();
        FireIndex fire=new FireIndex();
        JSONObject jsonValue = new JSONObject();
        Map<String,Object> map=new HashMap<String,Object>();
        //1查安全指数，2查消防指数
        if(type.equals("1")){
             safety=systemDao.querySafetyAll();
            /* map.put("活跃重点人员",safety.getActivityPerson());
             map.put("新发现",safety.getNewFind());
            map.put("陌生人员异常徘徊",safety.getStrangersZl());
            map.put("陌生人员异常滞留",safety.getStrangersPh());
            map.put("陌生人员骤增",safety.getStrangersZz());
            map.put("吸毒人员频繁夜间出没",safety.getXdrypfyjcm());
            map.put("感知发现外来前科",safety.getGzfxwlqk());
            map.put("上访对象串联聚会",safety.getSfdxcljh());
            map.put("漏管失控禁毒人员",safety.getLlskjdry());
            map.put("吸毒窝点研判",safety.getXdwdyp());
            map.put("主动发现可以XJ人员",safety.getZdfxkyxjry());
            map.put("发现XJ串联聚会",safety.getFxxjxljh());
            map.put("发现外来XJ人员",safety.getFxwlxjry());
            map.put("发现境外XJ人员",safety.getFxjwxjry());
            map.put("外来前科预警",safety.getWlqkyj());*/
            map.put("activityPerson",safety.getActivityPerson());
            map.put("newFind",safety.getNewFind());
            map.put("strangersZl",safety.getStrangersZl());
            map.put("strangersPh",safety.getStrangersPh());
            map.put("strangersZz",safety.getStrangersZz());
            map.put("xdrypfyjcm",safety.getXdrypfyjcm());
            map.put("gzfxwlqk",safety.getGzfxwlqk());
            map.put("sfdxcljh",safety.getSfdxcljh());
            map.put("llskjdry",safety.getLlskjdry());
            map.put("xdwdyp",safety.getXdwdyp());
            map.put("zdfxkyxjry",safety.getZdfxkyxjry());
            map.put("fxxjxljh",safety.getFxxjxljh());
            map.put("fxwlxjry",safety.getFxwlxjry());
            map.put("fxjwxjry",safety.getFxjwxjry());
            map.put("wlqkyj",safety.getWlqkyj());
            jsonValue=JSONObject.parseObject(JSONObject.toJSONString(map));
        }else{
             fire=systemDao.queryFireIndexAll();
             map.put("fireDeviceFailer",fire.getFireDeviceFailer());
             map.put("fireAlarm",fire.getFireAlarm());
            jsonValue=JSONObject.parseObject(JSONObject.toJSONString(map));
        }

        return jsonValue;
    }

    @Override
    public JSONObject UpdateOrInsertIndexService(JSONObject jsonObject) {
        JSONObject json=new JSONObject();
        String type=jsonObject.getString("type");
        Map<String,Object> map=new HashMap<String,Object>();
        map=JSONObject.toJavaObject(jsonObject,Map.class);
        //1为安全，2为消防
        if(type.equals("1")){
            systemDao.updateSafetyAll(map);
            json.put("message", "修改安全指数成功");
        }else{
            systemDao.updateFireAll(map);
            json.put("message", "修改消防指数成功");
        }
        json.put("status", "success");


        return json;
    }

    @Override
    public JSONObject addSysParam(Map<String, Object> paramMap) {

        JSONObject jsonObject = new JSONObject();
        try {
            List list = systemDao.querySysParams(paramMap);
            if(list!=null && list.size()>0){
                jsonObject.put("status", "failure");
                jsonObject.put("message", "操作失败，key已经存在");
                return jsonObject;
            }
            paramMap.put("mrowtime", ToolDateTime.getFormatDate());
            systemDao.addSysParam(paramMap);
            jsonObject.put("status", "success");
            jsonObject.put("message", "操作成功");

        } catch (Exception e) {
            log.error("SystemServiceImpl:addSysParam error ,param:{} ,e:",paramMap,e);
            jsonObject.put("status", "failure");
            jsonObject.put("message", e.getMessage());
        }

        return jsonObject;
    }

}
